//
//  StrategyViewController.m
//  BJS151148_ TeamProject_Travel
//
//  Created by lanou3g on 16/3/23.
//  Copyright © 2016年 lanou3g. All rights reserved.
//
#import "StrategyDetailTableViewController.h"
#import "StrategyCollectionViewCell.h"
#import "StrategyViewController.h"
#import "UIImageView+WebCache.h"
#import "StrategyFlowLayout.h"
#import "strategyModel.h"
#import "CDHttpHelper.h"
@interface StrategyViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UICollectionView * collectionView;
@property(nonatomic,strong)NSMutableArray * allDataArray;

@end

#define url @"https://chanyouji.com/api/destinations.json?page=1"

@implementation StrategyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    /*
     攻略主界面
     */
    
    self.allDataArray = [NSMutableArray array];
    StrategyFlowLayout *flowLayout = [[StrategyFlowLayout alloc]init];
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 2, 5, 10);
    //    flowLayout.footerReferenceSize = CGSizeMake(10, 10);
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.collectionView registerClass:[StrategyCollectionViewCell class] forCellWithReuseIdentifier:@"strategy"];
    [self handleData];
    [self.collectionView reloadData];
    
}

- (void)handleData{
    [[CDHttpHelper defaultCDHttpHelper] get:[NSString stringWithFormat:@"https://chanyouji.com/api/destinations.json?page=1"] params:nil success:^(id responseObj) {
        NSArray *array = [NSJSONSerialization JSONObjectWithData:responseObj options:NSJSONReadingMutableLeaves error:nil];
        for (NSDictionary *dict in array) {
            NSMutableArray *categoryArray = [NSMutableArray array];
            for (NSDictionary *dic in dict[@"destinations"]) {
                strategyModel *strModel = [strategyModel new];
                [strModel setValuesForKeysWithDictionary:dic];
                [categoryArray addObject:strModel];
            }
            [self.allDataArray addObject:categoryArray];
            
            [self.collectionView reloadData];
        }
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    StrategyDetailTableViewController *tableViewVC = [[StrategyDetailTableViewController alloc]init];
    strategyModel *strModel = self.allDataArray[indexPath.section][indexPath.item];
    tableViewVC.strModel = strModel;

    [self.navigationController pushViewController:tableViewVC animated:YES];
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    StrategyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"strategy" forIndexPath:indexPath];
    
    strategyModel *strModel = self.allDataArray[indexPath.section][indexPath.item];
    
    [cell.backImageView sd_setImageWithURL:[NSURL URLWithString:strModel.image_url]];
    cell.CNameLabel.text = strModel.name_zh_cn;
    cell.ENameLabel.text = strModel.name_en;
    cell.numLocationLabel.text = [NSString stringWithFormat:@"%ld 旅行地",strModel.poi_count];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeMake([UIScreen mainScreen].bounds.size.width, 50);
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [self.allDataArray[section] count];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.allDataArray.count;
}

- (CGFloat) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 2.0f;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake([UIScreen mainScreen].bounds.size.width / 2 - 13, 220);
}


@end
